//
//  MBDGroupModifyViewController.swift
//  MasterBoy
//
//  Created by kyson on 2023/3/19.
//

import Foundation
import YYText
import SnapKit

class MBDGroupModifyViewController: UIViewController {
    
    var tableView: UITableView!
    var fetchGroupButton: UIButton!
    var groupModel : MBDGroupDetailModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        groupModel = MBDGroupDetailModel()
        groupModel.groupName = "kyson的亿万俱乐部"
        groupModel.groupDesc = "夸夸群的群描述，字数限制1000个，夸夸群的群描述，支持表情包🙈，字数限制1000个，夸夸群的群描述，字数限制1000个，夸夸群的群描述，字数限制1000个，夸夸群的群描述，字数限制1000个，夸夸群的群描述，字数限制1000个，😇"
        
        self.view.backgroundColor = UIColor(colorHex: 0xF4F4F4)
        
        let rightItem = UIBarButtonItem(title: "编辑", style: UIBarButtonItem.Style.plain, target: self, action: #selector(rightItemEditPressed))
        self.navigationItem.rightBarButtonItem = rightItem
        
        tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.left.equalTo(self.view)
            make.top.equalTo(self.view)
            make.right.equalTo(self.view)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide).offset(-55)
        }
        
        tableView.backgroundColor = UIColor(colorHex: 0xF4F4F4)
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 100
        
        tableView.register(MBDGroupDescCell.self, forCellReuseIdentifier: "MBDGroupDescCell")
        tableView.register(MBDGroupPosterCell.self, forCellReuseIdentifier: "MBDGroupPosterCell")
        tableView.register(MBDGroupCodeCell.self, forCellReuseIdentifier: "MBDGroupCodeCell")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
      return .lightContent
    }
    
    @objc func rightItemEditPressed(){
        let rightItem = UIBarButtonItem(title: "保存", style: UIBarButtonItem.Style.plain, target: self, action: #selector(rightItemSavePressed))
        self.navigationItem.rightBarButtonItem = rightItem
    }
    
    @objc func rightItemSavePressed(){
        self.navigationController?.popViewController(animated: true)
    }

}

extension MBDGroupModifyViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        if(0 == indexPath.row) {
//            let cell = tableView.dequeueReusableCell(withIdentifier: "MBDGroupDescCell", for: indexPath)
//            (cell as! MBDGroupDescCell ).groupDesc = self.groupModel
//            return cell
//        } else
        
//        if (1 == indexPath.row) {
//            let cell = tableView.dequeueReusableCell(withIdentifier: "MBDGroupPosterCell", for: indexPath)
//            (cell as! MBDGroupPosterCell ).groupManager = self.groupModel.groupManager
//            return cell
//        } else
        if (2 == indexPath.row) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MBDGroupCodeCell", for: indexPath)
//            (cell as! MBDGroupCodeCell ).groupManager = self.groupModel.groupManager
            return cell
        }
        return UITableViewCell()
    }
}


extension MBDGroupModifyViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let viewModel = MBDGroupDetailViewModel(groupDetail: MBDGroupDetailModel())
        let vc = MBDGroupDetailViewController(viewModel: viewModel)
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
